package cn.edu.ctbu.websql.controller;

import cn.edu.ctbu.websql.domain.StSelect;
import cn.edu.ctbu.websql.helper.PageUtils;
import cn.edu.ctbu.websql.service.SelectService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

@Slf4j
@RestController
@RequestMapping(value = "/webapi/stselect")
public class SelectRestController {
    @Autowired
    private SelectService selectService;
    /**
     * 读取url：/webapi/students/list，显示学生列表
     * @return
     */
    @GetMapping("/list")
    public PageUtils GetAll(Integer limit, Integer page){
        Pageable pageable = PageRequest.of(page-1,limit);

        Page<StSelect> temSelect = selectService.getAll(pageable);
        PageUtils pageUtils = new PageUtils(temSelect.getTotalElements(),temSelect.getContent());
        return  pageUtils;
    }
    @GetMapping("/getdata/{id}")
    public Optional<StSelect> getData(@PathVariable Integer id){
        return selectService.getData(id);
    }
    @PutMapping("/update/{id}")
    public StSelect update(@PathVariable Integer id,StSelect newStSelect){
        // 在方法参数上使用 @Valid 注解，触发实体类的验证
        StSelect updateStSelect = selectService.update(id, newStSelect);
        return updateStSelect;
    }
    @GetMapping("/search/{no}")
    public List<StSelect> searchData(@PathVariable String no){
        return selectService.searchData(no);
    }

}
